package com.example.demo.Service;

import com.example.demo.Mappers.ExecGetOrderRegMapper;
import com.example.demo.Mappers.RegistMapper;
import com.example.demo.beans.*;
import com.example.demo.utils.AppConfigUtils;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**执行预约取号
 * Created by Administrator on 2018-04-24.
 */
@Service("ExecGetOrderRegService")
public class ExecGetOrderRegService {
    @Autowired
    private ExecGetOrderRegMapper emapper;
   @Autowired
    private RegistMapper mapper;
   @Transactional(readOnly = true)
    public ExecGetOrderRegResponse execOrder(ExecGetOrderRegRequest request)
    {
        GetOrderListResponseitem item=emapper.getOrerinfo(request.getRegID());
        OrderPatientInfo patientInfodetail=emapper.selectorderpatientinfo(request.getRegID());
        Ghxh_YwckBean ghxh_ywckBean=mapper.getGhxhYwxh();
        Jsjl_YwxhBean jsjl_ywxhBean=mapper.getJsjlywxh();
        String jsxh=jsjl_ywxhBean.getXh();
        String tempghxh=ghxh_ywckBean.getXh();
        int m=DateUtils.daysBetween(ghxh_ywckBean.getSsrq());
        int i=DateUtils.daysBetween(jsjl_ywxhBean.getSsrq());
        String czybm= AppConfigUtils.getGhybm();
        String czyks=AppConfigUtils.getGhyks();
        String ywckbm=AppConfigUtils.getYwckbm();
        String ghfsbm=AppConfigUtils.getGhfsbm();
        String fbbm=AppConfigUtils.getFbbm();
        String ghxh="";
        String jsjlid="";
        String ispay="0";
        String ghf="0";
        if(request.getTypeID().trim().equals("03")){ghf="2";}
        else if(request.getTypeID().trim().equals("08")){ghf="10";}
        else {ghf="1";}
        float fyhj=0;
        String DAY=DateUtils.converttimetoYYMMDDHH00(patientInfodetail.getYyghrq());
        System.out.print("这是医生编码："+request.getDoctID()+"这是日期"+DAY);
        DocDetailBean detailBean=mapper.selectdocdetail(request.getDoctID(),DAY);
        //门诊发票信息
        MzdqpjBean mzdqpjBean=mapper.Getmzpjxx(czybm);
        String qshnew=String.valueOf(Long.valueOf(mzdqpjBean.getQsh())+1);
        List<GhzlbmBean> list=mapper.selectghzl(request.getDoctID());
        Long tempyyghid=Long.parseLong(DateUtils.getStringToday()+"000000");
       /** if(item.getIsPay().equals("未收费")&&Long.parseLong(request.getPayAmt())>0){
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
        }  **/
        int q= DateUtils.daysBetween(patientInfodetail.getYyghrq());
        if(q<0&&item.getIsPay().equals("未付费")&&Long.parseLong(request.getPayAmt())>0){
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),patientInfodetail.getYyghid());
            return  regResponse;
        }
        if(item.getIsPay().trim().equals("已付费")){
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),patientInfodetail.getYyghid());
            return  regResponse;
        }
        System.out.print("只是q:"+q+"这是是否收费:"+item.getIsPay()+"这是交易金额："+Long.parseLong(request.getPayAmt())+"只是m："+m+"这是i:"+i);
        //当取号时间是当天并且以前没有交费时
        if(q==0&&item.getIsPay().trim().equals("未付费")&&Long.parseLong(request.getPayAmt())>0)
        { System.out.print("执行到挂号程序");
            if(m>=1&&i>=1){
            ghxh=ifmthanone(tempyyghid,request.getTypeID(),ghfsbm,fbbm,request.getPatientID(),czybm,czyks,
                    patientInfodetail.getBrnl(),patientInfodetail.getYyghrq(),ywckbm,request.getDoctID(),detailBean.getDeptID());
            for (int p=0;p<list.size();p++){
                mapper.savemzbbrfy(fbbm,ywckbm,list.get(p).getXlbm(),list.get(p).getMxfyxmbm(),czybm,list.get(p).getDlbm(),patientInfodetail.getYyghrq(),patientInfodetail.getBrid(),ghxh,patientInfodetail.getBrxm(),"1",
                        list.get(p).getFydj(),list.get(p).getFydj(),detailBean.getDeptID(),detailBean.getDoctID(),qshnew,jsjlid,"0","1");
                System.out.print("这是费用单价"+list.get(p).getFydj());
                fyhj=fyhj+Float.valueOf(list.get(p).getFydj());
            }
            String hj=String.valueOf(fyhj);
            mapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            jsjlid=ifithanone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getBrid(),czyks,patientInfodetail.getYlkh(),hj);
            mapper.updatebrfy(jsjlid,ghxh);
            mapper.updateyyclbz(patientInfodetail.getYyghid());
            PayDetailBean p=setPayBean(patientInfodetail,jsjlid,request);
            mapper.saverePaybean(p);
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),jsjlid);
            return  regResponse; }
        else if(m>=1&&i<1){
            ghxh=ifmthanone(tempyyghid,request.getTypeID(),ghfsbm,fbbm,request.getPatientID(),czybm,czyks,
                    patientInfodetail.getBrnl(),patientInfodetail.getYyghrq(),ywckbm,request.getDoctID(),detailBean.getDeptID());
            for (int p=0;p<list.size();p++){
                mapper.savemzbbrfy(fbbm,ywckbm,list.get(p).getXlbm(),list.get(p).getMxfyxmbm(),czybm,list.get(p).getDlbm(),patientInfodetail.getYyghrq(),patientInfodetail.getBrid(),ghxh,patientInfodetail.getBrxm(),"1",
                        list.get(p).getFydj(),list.get(p).getFydj(),detailBean.getDeptID(),detailBean.getDoctID(),qshnew,jsjlid,"0","1");
                System.out.print("这是费用单价"+list.get(p).getFydj());
                fyhj=fyhj+Float.valueOf(list.get(p).getFydj());
            }
            String hj=String.valueOf(fyhj);
            mapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            jsjlid=ifilessone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getBrid(),czyks,patientInfodetail.getYlkh(),jsxh ,hj);
            mapper.updatebrfy(jsjlid,ghxh);
            mapper.updateyyclbz(patientInfodetail.getYyghid());
            PayDetailBean p=setPayBean(patientInfodetail,jsjlid,request);
            mapper.saverePaybean(p);
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),jsjlid);
            return  regResponse; }
        else if(m<1&&i>=1){
            ghxh=ifmlessone(tempyyghid,request.getTypeID(),ghfsbm,fbbm,request.getPatientID(),czybm,czyks,
                    patientInfodetail.getBrnl(),patientInfodetail.getYyghrq(),ywckbm,request.getDoctID(),detailBean.getDeptID(),tempghxh);
            for (int p=0;p<list.size();p++){
                mapper.savemzbbrfy(fbbm,ywckbm,list.get(p).getXlbm(),list.get(p).getMxfyxmbm(),czybm,list.get(p).getDlbm(),patientInfodetail.getYyghrq(),patientInfodetail.getBrid(),ghxh,patientInfodetail.getBrxm(),"1",
                        list.get(p).getFydj(),list.get(p).getFydj(),detailBean.getDeptID(),detailBean.getDoctID(),qshnew,jsjlid,"0","1");
                System.out.print("这是费用单价"+list.get(p).getFydj());
                fyhj=fyhj+Float.valueOf(list.get(p).getFydj());
            }
            String hj=String.valueOf(fyhj);
            mapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            jsjlid=ifithanone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getBrid(),czyks,patientInfodetail.getYlkh(),hj);
            mapper.updatebrfy(jsjlid,ghxh);
            mapper.updateyyclbz(patientInfodetail.getYyghid());
            PayDetailBean p=setPayBean(patientInfodetail,jsjlid,request);
            mapper.saverePaybean(p);
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),jsjlid);
            return  regResponse; }
else { System.out.print("执行到都小于1");
System.out.print("病人姓名："+patientInfodetail.getBrxm());

            ghxh=ifmlessone(tempyyghid,request.getTypeID(),ghfsbm,fbbm,request.getPatientID(),czybm,czyks,
                    patientInfodetail.getBrnl(),patientInfodetail.getYyghrq(),
                    ywckbm,request.getDoctID(),
                    detailBean.getDeptID(),
                    tempghxh);
            for (int p=0;p<list.size();p++){
                mapper.savemzbbrfy(fbbm,ywckbm,list.get(p).getXlbm(),list.get(p).getMxfyxmbm(),czybm,list.get(p).getDlbm(),patientInfodetail.getYyghrq(),patientInfodetail.getBrid(),ghxh,patientInfodetail.getBrxm(),"1",
                        list.get(p).getFydj(),list.get(p).getFydj(),detailBean.getDeptID(),detailBean.getDoctID(),qshnew,jsjlid,"0","1");
                System.out.print("这是费用单价"+list.get(p).getFydj());
                fyhj=fyhj+Float.valueOf(list.get(p).getFydj());
            }
            String hj=String.valueOf(fyhj);
            mapper.updatemzdqqj(qshnew,mzdqpjBean.getQsh(),mzdqpjBean.getJsh(),mzdqpjBean.getFpjlid(),czybm);
            jsjlid=ifilessone(tempyyghid,ghxh,czybm,ywckbm,patientInfodetail.getBrid(),czyks,patientInfodetail.getYlkh(),jsxh,hj);
            mapper.updatebrfy(jsjlid,ghxh);
            mapper.updateyyclbz(patientInfodetail.getYyghid());
            PayDetailBean p=setPayBean(patientInfodetail,jsjlid,request);
            mapper.saverePaybean(p);
            emapper.updateorderlist(request.getRegID());
            emapper.UpdateOrderPay(request.getRegID());
            ExecGetOrderRegResponse regResponse=setOrderResponse(request,detailBean,"0",ghf,item.getTreatFee(),item.getOtherFee(),jsjlid);
return  regResponse;
        }

        }
        return  null;
    }
    /**
     * 当挂号业务序号表大于1
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     */
    private  String ifmthanone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
                               String czybm,String czyks,String brnl,String zcrq,String ywckbm,String ghks,String jzys){
        mapper.updateghxhywxh("000001",DateUtils.Getdnowdatexx());
        String  ghxh=String.valueOf(tempghxh+1);
        mapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
                ghks,brnl,zcrq,zcrq,ywckbm,jzys);
        return  ghxh;
    }
    /**
     * 当挂号时间等于当天时间
     * @param tempghxh
     * @param ghzlbm
     * @param ghfsbm
     * @param fbbm
     * @param brid
     * @param czybm
     * @param czyks
     * @param brnl
     * @param zcrq
     * @param ywckbm
     * @param xh
     * @return
     */
    private  String ifmlessone(Long tempghxh,String ghzlbm,String ghfsbm,String fbbm,String brid,
                               String czybm,String czyks,String brnl,String zcrq,String ywckbm,String xh,String ghks,String jzys){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=mapper.selectywxhbyid(xh1).getXh();
        mapper.updateghxhywxh(xh2,DateUtils.Getdnowdatexx());
        String  ghxh=String.valueOf(tempghxh+xh1);
        mapper.saveghb(ghxh,ghzlbm,ghfsbm,fbbm,brid,czybm,czyks,
                ghks,brnl,zcrq,zcrq,ywckbm,jzys);
        return  ghxh;
    }
    /**
     * 当结算记录业务序号时间大于1时执行
     * @param tempjsjlid
     * @param ghxh
     * @param czybm
     * @param ywckbm
     * @param brid
     * @param czyks
     * @param ylkh
     * @return
     */
    private String  ifithanone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh,String fyhj){
        mapper.updejsjlywxh("000001",DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,fyhj,"0",
                fyhj,"0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        mapper.saveghjsb(jsjlBean);
        return  jsjlid;
    }

    private String  ifilessone(Long tempjsjlid,String ghxh,String czybm,String ywckbm,String brid,String czyks,String ylkh,String xh,String fyhj){
        int xh1 = Integer.parseInt(xh.trim()) + 1;
        String xh2=mapper.selectywxhbyid(xh1).getXh();
        mapper.updejsjlywxh(xh2,DateUtils.Getdnowdatexx());
        String jsjlid=String.valueOf(tempjsjlid+xh1);
        JsjlBean jsjlBean=new JsjlBean(jsjlid,ghxh,"1",czybm,ywckbm,brid,fyhj,"0",
                fyhj,"0","0","0" ,"0","0",DateUtils.Getdnowdatexx(),czyks,
                "挂号结算","","0","0","0",ylkh,"0",
                "0","0","0");
        mapper.saveghjsb(jsjlBean);
        return  jsjlid;
    }

    private  PayDetailBean setPayBean(  OrderPatientInfo pa,String jsxh ,ExecGetOrderRegRequest r){
        PayDetailBean pay=new PayDetailBean();
        pay.setBzms("微信挂号结算");
        pay.setAddress(pa.getJtzz());
        pay.setAutoTranNo(r.getAutoTranNo());
        pay.setBankCardNo(r.getBankCardNo());
        pay.setBankPNo(r.getBankPNo());
        pay.setBankReferNo(r.getBankReferNo());
        pay.setBankTermid(r.getBankTermid());
        pay.setBirthday(pa.getBrsr());
        pay.setCardType(r.getCardType());
        pay.setCodeBuyerAccount(r.getCodeBuyerAccount());
        pay.setBankTranNo(r.getBankTranNo());
        pay.setJsxh(jsxh);
        pay.setCodeTradeOrderNo(r.getCodeTradeOrderNo());
        pay.setMobile(pa.getSj());
        pay.setName(pa.getBrxm());
        pay.setPayAmt(r.getPayAmt());
        pay.setIDCardNo(pa.getYlkh());
        pay.setSex(pa.getBrxb());
        pay.setUserId(r.getUserID());
        pay.setPayTranTime(r.getPayTranTime());
        pay.setCardNo(r.getCardNo());
        pay.setPatientID(pa.getBrid());

        return  pay;
    }
   private ExecGetOrderRegResponse setOrderResponse(ExecGetOrderRegRequest request, DocDetailBean detailBean,
   String appFee,String RegFee,String TreatFee,String OtherFee,String InvoiceNo )
   {ExecGetOrderRegResponse regResponse=new ExecGetOrderRegResponse(detailBean.getTypeName(),
  detailBean.getDepName(),detailBean.getDoctName(),detailBean.getRankName(),appFee,RegFee,TreatFee,OtherFee,
 detailBean.getSpecialty(),detailBean.getTreatLocation(),detailBean.getTimeRegion(),detailBean.getWaitNum(),
 DateUtils.Getdnowdatexx(),InvoiceNo,InvoiceNo);
 return  regResponse;
   }


}
